package com.hibikiluler.javalearning.exception;

import com.hibikiluler.javalearning.util.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.http.HttpStatus;
import org.springframework.security.core.userdetails.UsernameNotFoundException;
import org.springframework.security.web.access.AccessDeniedHandler;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseStatus;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.nio.file.AccessDeniedException;

/**
 *
 */

@Slf4j
@RestControllerAdvice
public class GlobleExpection {
    @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR) //抛出异常代码
    @ExceptionHandler(value = RuntimeException.class)
    public Result exception(RuntimeException e) {
        e.printStackTrace();
        log.error("系统运行时异常---》{}",e.getMessage());
        return Result.fail(e.getMessage()); //返回异常信息
    }

    @ResponseStatus(HttpStatus.FORBIDDEN)
    @ExceptionHandler(value = AccessDeniedException.class)
    public Result exception(AccessDeniedException e){
        log.error("权限不足--->{}",e.getMessage());
        return  Result.fail("权限不足！");
    }

    @ResponseStatus(HttpStatus.BAD_REQUEST)
    @ExceptionHandler(value = UsernameNotFoundException.class)
    public Result exception(UsernameNotFoundException e){
        log.error("用户名未找到 --》 {}",e.getMessage());
        return Result.fail(e.getMessage());
    }
}
